package com.abss.vote.controller;

import com.abss.vote.model.Administrators;
import com.abss.vote.model.Comment;
import com.abss.vote.service.CommentService;
import com.abss.vote.util.ConentChackUtil;
import com.abss.vote.util.JsonUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 评论
 */
@Controller
public class CommentController {

    @Autowired
    private CommentService commentService;





    @RequestMapping("/toComment")
    public String toComment(Integer WORK_ID, Model model,HttpSession session){
        session.setAttribute("WORK_ID", WORK_ID);
        model.addAttribute("WORK_NAME",commentService.getWorkName(WORK_ID));
        return "back/index/comment_management";
    }


    /**
     * 后台获取评论
     * @param page
     * @param limit
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping("/getBackComments")
    public String getBackComments(int page,int limit,HttpSession session){
        Integer WORK_ID = (Integer)session.getAttribute("WORK_ID");
        HashMap<String,Object> map = new HashMap<>();
        int counts = commentService.getCount(WORK_ID);
        PageInfo<Comment> comments = commentService.getCommentsBack(page,limit,WORK_ID);
        map.put("code",0);
        map.put("count",counts);
        map.put("data",comments.getList());
        return JsonUtil.objectToJson(map);
    }

    /**
     * 后台进行留言
     * @param CONENT
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping("/setCommentBack")
    public int setCommentBack(String CONENT,HttpSession session){
        boolean res = false;
        try {
            res = ConentChackUtil.chackConent(CONENT);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(res){
            Integer WORK_ID = (Integer)session.getAttribute("WORK_ID");
            Administrators ad =(Administrators)session.getAttribute("ADMIN");
            return commentService.setCommentBack(CONENT,WORK_ID,ad);
        }
        return 0;

    }

    /**
     * 后台进行回复
     * @param CONENT
     * @param ID
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping("/setReplyBack")
    public int setReplyBack(String CONENT,Integer ID,String BE_USER_ID,HttpSession session){
        boolean res = false;
        try {
            res = ConentChackUtil.chackConent(CONENT);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(res){
            Integer WORK_ID = (Integer)session.getAttribute("WORK_ID");
            Administrators ad =(Administrators)session.getAttribute("ADMIN");
            return commentService.setReplyBack(CONENT,WORK_ID,ad,ID,BE_USER_ID);
        }
        return 0;
    }
/***********************************************************************************************************************************/

    /**
     * 获取某个作品的评论
     * @param WORK_ID
     * @return
     */
    @ResponseBody
    @RequestMapping("/getComments")
    public Map<String,Object> getComments(@RequestParam(required=false,defaultValue="1")int start, @RequestParam(required=false,defaultValue="5")int count, Integer WORK_ID){
        List<Comment> comments = commentService.getComments(start,start+count-1,WORK_ID);
        Map<String,Object> ret=new HashMap<String,Object>();
        ret.put("code",1);
        ret.put("data",comments);
        return ret;
    }

    /**
     * 发表评论
     * @param comment
     * @return
     */
    @ResponseBody
    @RequestMapping("/Comment")
    public int insertComment(Comment comment){
        boolean res = false;
        try {
            res = ConentChackUtil.chackConent(comment.getCONENT());
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(res){
            return commentService.insertComment(comment);
        }
        return 0;
    }

    /**
     * 是否是作者
     * @param comment
     * @return
     */
    @ResponseBody
    @RequestMapping("/IsAuthor")
    public Boolean IsAuthor(Comment comment){
        return commentService.IsAuthor(comment);
    }

    /**
     * 发表回复
     * @param comment
     * @return
     */
    @ResponseBody
    @RequestMapping("/Reply")
    public int insertReply(Comment comment){
        boolean res = false;
        try {
            res = ConentChackUtil.chackConent(comment.getCONENT());
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(res){
            return commentService.insertReply(comment);
        }
        return 0;
    }


}
